const mongoose = require('mongoose'),
  dataApi=require('./common');

const EMAIL = /^['_A-Za-z0-9-]+(\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$/;

/* wm_posts table structure
 --------------------------------------------
 - user_email : 用户邮箱
 - user_nickname : 用户昵称
 - user_password : 用户密码
 - user_status : 用户状态, 0正常状态 -1禁用
 - user_role : 用户权限, 0是普通用户 1是管理员
 - user_registered : 注册时间
 --------------------------------------------
 */

const UserSchema = new mongoose.Schema({

    user_email: {
        type: String,
        validate: [
            {
                validator: function (val) {
                    return EMAIL.test(val);
                },
                msg: '请输入正确的邮箱格式'
            }
        ],
        required:true,
        index: true,
        unique: true
    },

    user_nickname: {
        type: String
    },

    user_password: {
        type: String,
        required:true
    },

    user_role: {
        type: Number,
        default: 0
    },

    user_status: {
        type: Number,
        default: 0
    },

    user_registered: {
        type: String,
        default: Date.now
    }
});

dataApi(UserSchema,'user');

module.exports = mongoose.model('user', UserSchema);